<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('电费管理')" />
    <th:block th:include="include :: select2-css" />
</head>
<body class="gray-bg">
     <div class="container-div">
        <div class="row">
            <div class="col-sm-12 search-collapse">
                <form id="formId">
                    <div class="select-list">
                        <ul>
                            <li>
                                <label>电费批次：</label>
                                <input autocomplete="off" type="text" placeholder="请填写批次"  name="etldt" />
                            </li>
                            <li>
                                <label>楼层：</label>
                                <select name="floorName" class="form-control" id="floorName" required>
                                    <option value="">请选择</option>
                                    <option th:each="floorInfo:${floorInfos}" th:text="${floorInfo.floorName}" th:value="${floorInfo.floorName}"></option>
                                </select>
                            </li>
                            <li>
                                <a class="btn btn-primary btn-rounded btn-sm" onclick="query()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                                <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
                            </li>
                        </ul>
                    </div>
                </form>
            </div>
            <div class="btn-group-sm" id="toolbar" role="group">
                <a class="btn btn-success" onclick="generateElec()" shiro:hasPermission="sujian:elecCost:caculate">
                    <i class="fa fa-plus"></i> 计算
                </a>
                <!--<a class="btn btn-success" onclick="importExcel()" shiro:hasPermission="sujian:elecCost:import">-->
                    <!--<i class="fa fa-upload"></i> 导入-->
                <!--</a>-->
                <a class="btn btn-warning" onclick="$.table.exportExcel()" shiro:hasPermission="sujian:elecCost:export">
                    <i class="fa fa-download"></i> 导出
                </a>
            </div>
            <div class="col-sm-12 select-table table-striped">
                <table id="bootstrap-table"></table>
            </div>
        </div>
    </div>
     <th:block th:include="include :: footer" />
     <th:block th:include="include :: bootstrap-fileinput-js" />
     <th:block th:include="include :: select2-js" />
    <script th:inline="javascript">

        var prefix = ctx + "sujian/elecCost";

        $(function () {
            // initEtldt()
        })

        function generateElec() {
            layer.open({
                type:1,
                title:["选择计算电费月份","font-size:18px"],
                area:["400px","180px"],
                shade:false,
                anim:3,
                shade:0.3,
                content:$("#etldtArea").html(),
                btn:['确认'],
                yes:function (index) {
                    var etldt = $('#generateEtldt').val();
                    if(etldt==null||etldt==""||etldt==undefined){
                        $.modal.alertError("请输入要计算电费的年月")
                        return;
                    }
                    $.ajax({
                        url:prefix+"/generateElec",
                        type : "POST",
                        data : {
                            "etldt": etldt,
                        },
                        beforeSend: function () {
                            $.modal.loading("正在处理中，请稍后...");
                            $.modal.disable();
                        },
                        error : function(request) {
                            $.modal.alertError("系统错误");
                            $.modal.closeLoading();
                        },
                        success : function(data) {
                            $.modal.closeLoading();
                            $.operate.successCallback(data);
                        }
                    })
                }
            })

        }

        var options;
        function query(){
            if(options!=null&&options!=undefined){
                var params = $("#bootstrap-table").bootstrapTable('getOptions');
                $("#bootstrap-table").bootstrapTable('refresh', params);
            }else{
                options = {
                    url: prefix + "/list",
                    removeUrl: prefix + "/remove",
                    exportUrl: prefix + "/export",
                    importUrl: prefix + "/import",
                    importTemplateUrl: prefix + "/importTemplate",
                    uniqueId:"stfId",
                    modalName: "电费信息",
                    columns: [{
                        checkbox: true
                    },
                        {
                            field: 'elecId',
                            title: '电费Id',
                            visible: false
                        },
                        {
                            field: 'floorName',
                            title: '楼层'
                        },
                        {
                            field: 'etldt',
                            title: '月份'
                        },
                        {
                            field: 'tenantId',
                            title: '租户Id',
                            visible: false
                        },
                        {
                            field: 'tenantName',
                            title: '租户名'
                        },
                        {
                            field: 'degreeUsed',
                            title: '用电量'
                        },
                        {
                            field: 'lightDegree',
                            title: '照明电量'
                        },
                        {
                            field: 'lightCost',
                            title: '照明费用（元）'
                        },
                        {
                            field: 'powerDegree',
                            title: '动力电量'
                        },
                        {
                            field: 'powerCost',
                            title: '动力费用（元）'
                        }]
                };
                $.table.init(options);
            }
        }
        function importExcel(formId, width, height) {
            table.set();
            layer.open({
                type: 1,
                area: ['400px','230px'],
                fix: false,
                //不固定
                maxmin: true,
                shade: 0.3,
                title: '导入' + table.options.modalName + '数据',
                content: $('#importTpl').html(),
                btn: ['<i class="fa fa-check"></i> 导入', '<i class="fa fa-remove"></i> 取消'],
                // 弹层外区域关闭
                shadeClose: true,
                btn1: function(index, layero){
                    var file = layero.find('#elecFileCost').val();
                    if (file == '' || (!$.common.endWith(file, '.xls') && !$.common.endWith(file, '.xlsx'))){
                        $.modal.msgWarning("请选择后缀为 “xls”或“xlsx”的文件。");
                        return false;
                    }
                    var index = layer.load(2, {shade: false});
                    $.modal.disable();
                    var formData = new FormData(layero.find('form')[0]);
                    $.ajax({
                        url:prefix + "/importExcel",
                        data: formData,
                        cache: false,
                        contentType: false,
                        processData: false,
                        type: 'POST',
                        success: function (result) {
                            if (result.code == web_status.SUCCESS) {
                                $.modal.closeAll();
                                $.modal.alertWarning(result.msg);
                                $.table.refresh();
                            } else {
                                layer.close(index);
                                $.modal.enable();
                                $.modal.alertError(result.msg);
                            }
                        }
                    });
                }
            });
        }

        // function initEtldt() {
        //     var date = new Date();
        //     var year = date.getFullYear();
        //     var month = date.getMonth() + 1;
        //     if(month == 0){
        //         month = 12;
        //         year = year - 1;
        //     }
        //     if(month < 10){
        //         month = "0" + month;
        //     }
        //     $('#etldt').val(""+year+month);
        // }

    </script>
</body>

<!-- 导入区域 -->

<script id="importTpl" type="text/template">
    <form enctype="multipart/form-data" class="mt20 mb10">
        <div class="col-xs-offset-1">
            <input type="file" id="elecFileCost" name="file"/>
            <div class="mt10 pt5">
                <a onclick="$.table.importTemplate()" class="btn btn-default btn-xs"><i class="fa fa-file-excel-o"></i> 下载模板</a>
            </div>
            <span style="color: red;">若导入月份已有电费记录，将直接覆盖！</span>
            <font color="red" class="pull-left mt10">
                提示：仅允许导入“xls”或“xlsx”格式文件！！！
            </font>
        </div>
    </form>
</script>
<script type="text/template" id="etldtArea">
    <form class="mt20 mb10">
        <div style="margin: 20px;">
            <span style="color: red;">若选择月份已有电费记录，将直接覆盖！</span>
            <input type="text" class="form-control" id="generateEtldt" placeholder="请输入要计算的电费月份（yyyyMM）">
        </div>
    </form>
</script>
</html>